package com.jye.rseven.user.config.Interceptor;

import com.alibaba.fastjson.JSON;
import com.jye.rseven.user.util.JwtUtils;
import com.jye.rseven.user.util.RedisUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;


public class LoginInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");

        if(StringUtils.isEmpty(token)){
            token = request.getParameter("token");
        }
        System.out.println(token);
//        System.out.println("preHandle............");
        if(!StringUtils.isEmpty(token)){
            //有token
            try{
                Claims claims = JwtUtils.checkJWT(token);
//                System.out.println(token);
//                System.out.println("claims"+claims);
                return true;
            }catch (Exception e){
                response.setStatus(401);
                return false;
            }
        }else{
            //无token

            response.setStatus(401);
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {

    }
}
